<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-interface-add">
			<input name="moduleId"  type="hidden" id="treeId"/>
			<div class="form-group">
				<label class="col-sm-3 control-label">所属模块：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="moduleName" onclick="selectModuleTree()" readonly="true" id="treeName">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label ">接口名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" id="interfaceName" name="interfaceName"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label ">请求地址：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" id="url" name="url"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">请求方式：</label>
				<div class="col-sm-8">
					<select id="reqType" class="form-control m-b" th:with="type=${@dict.getType('sys_interface_type')}">
						<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">请求协议：</label>
				<div class="col-sm-8">
					<select id="reqProtocol" class="form-control m-b" th:with="type=${@dict.getType('sys_interface_protocol')}">
						<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">请求数据格式：</label>
				<div class="col-sm-8">
					<select id="reqDataType" class="form-control m-b" th:with="type=${@dict.getType('sys_interface_reqdatatype')}">
						<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">响应数据格式：</label>
				<div class="col-sm-8">
					<select id="respDataType" class="form-control m-b" th:with="type=${@dict.getType('sys_interface_respdatatype')}">
						<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">接口标识：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" id="operation" name="operation"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">开发状态：</label>
				<div class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('sys_interface_devstatus')}">
						<input type="radio" th:id="${dict.dictId}" name="status" th:value="${dict.dictValue}" th:checked="${dict.isDefault == 'Y' ? true : false}">
						<label th:for="${dict.dictId}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">状态：</label>
				<div class="col-sm-8">
					<div class="onoffswitch">
                         <input type="checkbox" th:checked="true" class="onoffswitch-checkbox" id="status" name="status">
                         <label class="onoffswitch-label" for="status">
                             <span class="onoffswitch-inner"></span>
                             <span class="onoffswitch-switch"></span>
                         </label>
                     </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">排序号：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" id="orderNum" name="orderNum"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input id="remark" name="remark" class="form-control" type="text">
				</div>
			</div>
		</form>
	</div>
	<div th:include="include::footer"></div>
	<script th:src="@{/ajax/libs/select/select2.js}"></script>
	<script>
        $("#form-interface-add").validate({
        	rules:{
        		url:{
        			required:true,
        			remote: {
                        url: ctx + "system/user/checkLoginNameUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                        	name : function() {
                                return $.common.trim($("#url").val());
                            }
                        },
                        dataFilter: function(data, type) {
                        	return $.validate.unique(data);
                        }
                    }
        		},
				moduleName:{ required:true, },
				interfaceName:{ required:true, },
				reqType:{ required:true, },
				reqProtocol:{ required:true, },
				reqDataType:{ required:true, },
				orderNum:{ required:true, },
        	},
        	messages: {
                "url": {
                    remote: "该请求地址已经存在"
                }
            }
        });
        
        function submitHandler() {
	        if ($.validate.form()) {
	        	add();
	        }
	    }

        function add() {
        	var userId = $("input[name='userId']").val();
        	var deptId = $("input[name='deptId']").val();
        	var account = $("input[name='account']").val();
        	var userName = $("input[name='userName']").val();
        	var password = $("input[name='password']").val();
        	var email = $("input[name='email']").val();
        	var phone = $("input[name='phone']").val();
        	var sex = $("#sex option:selected").val();
        	var status = $("input[name='status']").is(':checked') == true ? 0 : 1;
        	var roleIds = $.form.selectCheckeds("role");
        	$.ajax({
        		cache : true,
        		type : "POST",
        		url : ctx + "system/user/add",
        		data : {
        			"userId": userId,
        			"deptId": deptId,
        			"loginName": loginName,
        			"userName": userName,
        			"passWord": password,
        			"email": email,
        			"phone": phone,
        			"sex": sex,
        			"status": status,
        			"roleIds": roleIds
        		},
        		async : false,
        		error : function(request) {
        			$.modal.alertError("系统错误");
        		},
        		success : function(data) {
        			$.operate.successCallback(data);
        		}
        	});
        }

        /*接口管理-新增-选择模块树*/
        function selectModuleTree() {
        	var treeId = $("#treeId").val();
        	var moduleId = $.common.isEmpty(treeId) ? "0" : $("#treeId").val();
        	var url = ctx + "system/module/selectModuleTree/" + moduleId;
			var options = {
				title: '选择部门',
				width: "380",
				url: ctx + "system/module/selectModuleTree/" + moduleId,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			// if ($.tree.notAllowParents(tree)) {
				var body = layer.getChildFrame('body', index);
    			$("#treeId").val(body.find('#treeId').val());
    			$("#treeName").val(body.find('#treeName').val());
    			layer.close(index);
			// }
		}
    </script>
</body>
</html>
